/*
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-12-30 15:50:56
 * @LastEditors: 朱安武 549410045@qq.com
 * @LastEditTime: 2023-01-06 16:04:15
 * @FilePath: \html学习\vue-test\elementUi\src\router.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
    createRouter,
    createWebHistory,
} from 'vue-router'

import LoginPage from "@pages/LoginPage.vue";
import MainPage from "@pages/MainPage.vue";
import BannerPage from "@pages/BannerPage.vue"
import EditorPage from '@pages/EditorPage.vue'
import MainHome from './components/MainHome.vue'
import EchartsPage from '@pages/EchartsPage.vue'
import { ElMessage } from 'element-plus';

export const router = createRouter({
    history:createWebHistory(),
    routes:[
        //重定向
        // {
        //     path:'/',
        //     redirect:'/login'
        // },
        {
            path:'/login',
            //别名
            alias:'/',
            component:LoginPage,
           
        },
        // {
        //     path:'/MainAsideHome',
        //     component:()=>import('./components/MainAsideHome.vue')
        // },
        {
            path:'/mainPage',
            component:MainPage,
            //路由守卫
            beforeEnter(to,from){
                const token = localStorage.getItem('token')
                const adminname = localStorage.getItem('adminname')
                //判断localStorage里的值
                if(!token || !adminname) {
                  //返回一个path 自动跳转
                return '/login'
                }
            },
            children:[
                {
                    path:'MainHome',
                    component:MainHome
                },
                {
                    path:'bannerlist',
                    component:BannerPage
                },
                {
                    path:'editor',
                    component:EditorPage
                },
                {
                    path:'echart',
                    component:EchartsPage
                }
            ]

        }
    ]
})

//全局守卫  取反 to.path !== '/login' 判断无token

//获取所有path
export const pathList = []
//加path
function addPath(obj){
    obj.forEach(item =>{
    pathList.push(item.path?item.path:item)
    if(item.children){
        return addPath(item.children.map(item => '/mainPage/'+item.path))
        }
    }
)}
addPath(router.options.routes)

// router.beforeEach((to,from)=>{
//     //判断to的path
//     if(pathList.includes(to.path)){
//         if(to.path === '/login') return
//         const token = localStorage.getItem('token')
//         const adminname = localStorage.getItem('adminname')
//         //判断localStorage里的值
//         if(!token || !adminname) {
//             //返回一个path 自动跳转
//             return '/login'
//         }
//     } 
//     // else {
//     //     ElMessage.error('地址不存在');
//     // }              
// })
